$(function () {
  // //打印初始化
  $("#print").click(function () {
    $("#printbox").jqprint();
  });
  //导出word
  $("#word").click(function () {
    $("#printbox").wordExport();
  })
});
//n为你要传入的参数，当前为0，前一天为-1，后一天为1
function getBeforeDate(n) {
  var date = new Date();
  var year, month, day;
  date.setDate(date.getDate() + n);
  year = date.getFullYear();
  month = date.getMonth() + 1;
  day = date.getDate();
  s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
  return s;
}
//获取指定日期前七天
function getBeforeWeek(d) {
  d = new Date(d);
  d = +d - 1000 * 60 * 60 * 24 * 7;
  d = new Date(d);
  var year = d.getFullYear();
  var mon = d.getMonth() + 1;
  var day = d.getDate();
  s = year + "-" + (mon < 10 ? ('0' + mon) : mon) + "-" + (day < 10 ? ('0' + day) : day);
  return s;
}

function getBase($scope, CertusFormAjax) {
  var deptIds = "";
  var compIds = "";
  $("#beginTime").val(getBeforeDate(-7));
  $("#endTime").val(getBeforeDate(0));
  this.getDepts = function () {
    var company = $("#company").val();
    if (company != "") {
      findCompDepts(company, deptIds);
    } else {
      findCompDepts(compIds, deptIds);
    }
  };

  this.findCompDepts = function (company, depts) {
    CertusFormAjax.ajax("findCompDepts", { "compCodes": company, "deptIds": depts,"isBm":"BM" }, function (response) {
      var data = response.data;
      $scope.dept = data;
    });
  };

  this.findAllDepts = function (dept) {
    CertusFormAjax.ajax("findQDepts", { "deptIds": dept }, function (response) {
      var data = response.data;
      $scope.dept = data;
    });
  };

  this.getChildDept = function (superDept) {
    CertusFormAjax.ajax("childDeptList", { "deptCode": deptIds, "pdeptId": superDept }, function (response) {
      $scope.childDept = response.data;
    });
  };
  CertusFormAjax.ajax("isCrcd", {
    "code": "XMJZ"
  }, function (response) {
    var data = JSON.parse(response.data);
    deptIds = data.deptCode;
    compIds = data.comanyCode;
    if (data.comanyCode != "") {
      CertusFormAjax.ajax("companyListByCode", { "code": data.comanyCode }, function (response) {
        $scope.comp = JSON.parse(response.data);
      });
      findCompDepts(data.comanyCode, deptIds);
    } else {
      $("#company").attr("disabled", "disabled");
      findAllDepts(data.deptCode);
    }

    // if (data.isTrue == true) {
    //     searchs("", "1", deptIds,compIds, getBeforeDate(-2), getBeforeDate(2));
    // } else {
    //     CertusFormAjax.ajax("getDeptCode", {}, function (response) {
    //         var data1 = response.data;
    //         $scope.deptCode = data1;
    //         $("#deptCode").attr("disabled", "disabled");
    //         $("#deptFlag").html("归属");
    //         //默认查询
    //         searchs(data1, "1", deptIds,compIds, getBeforeDate(-8), getBeforeDate(2));
    //     });
    // }
  });

  this.search = function (company, superDeptCode, childDeptCode, beginTime, endTime) {
    CertusFormAjax.ajax('zbTargetList', {
      "company": company, "superDeptCode": superDeptCode,
      "childDeptCode": childDeptCode, "beginTime": beginTime, "endTime": endTime
    }, function (response) {
      $scope.target = response.data;

      var trsize = $scope.target.length;
      var target = $scope.target;
      if(trsize != 0){
        var html;
        for(i = 0;i < trsize; i++){
          html += '<tr>'+
              '<td>'+ target[i].deptName +'</td>'+
              '<td>'+ target[i].proName +'</td>'+
              '<td>'+ target[i].proTarget +'</td>'+
              '<td>'+ target[i].money +'</td></tr>'

        }
          $("#target").html(html);
      }
    });
    // CertusFormAjax.ajax('findTjnum',{"company":company,"superDeptCode":superDeptCode,
    //     "childDeptCode":childDeptCode,"beginTime":beginTime,"endTime":endTime},function (response) {
    //     var data = response.data;
    //     $scope.xsnum = data.xsnum;
    //     $scope.lxnum = data.lxnum;
    //     $scope.tbnum = data.tbnum;
    //     $scope.htnum = data.htnum;
    //     $scope.jfnum = data.jfnum;
    // });

    CertusFormAjax.ajax('findbbCrList', {
      "company": company, "superDeptCode": superDeptCode,
      "childDeptCode": childDeptCode, "beginTime": beginTime, "endTime": endTime
    }, function (response) {
      var data = response.data;
      $scope.crlist = data.crList;
      $scope.crsize = data.size;

      var trsize = $scope.crlist.length;
      var crlist = $scope.crlist;
      if(trsize != 0){
        var html
        for(i = 0;i < trsize; i++){
              html += '<tr>'+
                  '<td>'+ crlist[i].pro_name +'</td>'+
                  '<td>'+ crlist[i].crtype +'</td>'+
                  '<td>'+ crlist[i].content +'</td>'+
                  '<td>'+ getLocalTime(crlist[i].create_time) +'</td></tr>'
        }
      }
        $("#crlist").html(html);
  });
  CertusFormAjax.ajax('findZdgzList', {
    "company": company, "superDeptCode": superDeptCode,
    "childDeptCode": childDeptCode, "beginTime": beginTime, "endTime": endTime
  }, function (response) {
    $scope.zdgzlist = response.data;

    var trsize = $scope.zdgzlist.length;
      var zdgzlist = $scope.zdgzlist;
      if(trsize != 0){
        var html;
        for(i = 0;i < trsize; i++) {
            html += '<tr>' +
                '<td>' + zdgzlist[i].proName + '</td>' +
                '<td>' + zdgzlist[i].krsDesc + '</td>' +
                '<td>' + zdgzlist[i].rwDesc + '</td>' +
                '<td>' + getLocalTime(zdgzlist[i].updateTime) + '</td></tr>'
        }
        $("#zdgzlist").html(html);
      }
  });

  //查询项目数量
  CertusFormAjax.ajax('findBbPronums', {
    "company": company, "superDeptCode": superDeptCode,
    "childDeptCode": childDeptCode
  }, function (response) {
    var data = response.data;
    var qtnums = 0;
    for (var i = 0; i < data.length; i++) {
      if (data[i].proType == "BM") {
        $scope.bmtotalNum = data[i].count;
      } else if (data[i].proType == "XS") {
        $scope.xstotalNum = data[i].count;
      } else {
        qtnums += data[i].count;
      };
    }
    if ($scope.bmtotalNum == undefined) {
      $scope.bmtotalNum = 0;
    }
    if ($scope.xstotalNum == undefined) {
      $scope.xstotalNum = 0;
    }
    $scope.qttotalNum = qtnums;
  });
  CertusFormAjax.ajax('findBbProgxNums', {
    "company": company, "superDeptCode": superDeptCode,
    "childDeptCode": childDeptCode, "beginTime": beginTime, "endTime": endTime
  }, function (response) {
    var data = response.data;
    var qtgxnums = 0;
    var bmnums = 0;
    var xsnums = 0;
    for (var i = 0; i < data.length; i++) {
      if (data[i].type == "BM") {
        bmnums = data[i].count;
      } else if (data[i].type == "XS") {
        xsnums = data[i].count;
      } else {
        qtgxnums += data[i].count;
      }
    };
    $scope.qtgxNum = qtgxnums;
    $scope.bmgxNum = bmnums;
    $scope.xsgxNum = xsnums;
  });
  CertusFormAjax.ajax('getProStageNums', {}, function (response) {
    var data = response.data;
    var xsnum = 0;
    var lxnum = 0;
    var tbnum = 0;
    var htnum = 0;
    var jfnum = 0;
    for (var i = 0; i < data.length; i++) {
      if (data[i].stageCode == 1) {
        xsnum = data[i].count;
      } else if (data[i].stageCode == 2) {
        lxnum = data[i].count;
      } else if (data[i].stageCode == 3) {
        tbnum = data[i].count;
      } else if (data[i].stageCode == 4) {
        htnum = data[i].count;
      } else if (data[i].stageCode == 5) {
        jfnum = data[i].count;
      }
    }
    $scope.xsnum = xsnum;
    $scope.lxnum = lxnum;
    $scope.tbnum = tbnum;
    $scope.htnum = htnum;
    $scope.jfnum = jfnum;
  });
  //查询本周竞标信息
  CertusFormAjax.ajax('findBids', {
    "company": company, "superDeptCode": superDeptCode,
    "childDeptCode": childDeptCode, "beginTime": beginTime, "endTime": endTime
  }, function (response) {
    var data = response.data;
    $scope.tbnums = data.tbnum;
    $scope.zbnums = data.zbnum;
    $scope.lbnums = data.lbnum;
  });

  //查询问题与决策数量
  CertusFormAjax.ajax('findBbRdCount', {
    "company": company, "superDeptCode": superDeptCode,
    "childDeptCode": childDeptCode
  }, function (response) {
    $scope.wjNum = response.data;
  });
};

//查询
$(".searchs").on('click', function () {
  // 开启
  var index = layer.load(1);
  var beginTime = $("#beginTime").val();
  var endTime = $("#endTime").val();
  var company = $("#company").val();
  var superDeptCode = $("#deptCode").val();
  var childDeptCode = $("#childDeptCode").val();
  search(company, superDeptCode, childDeptCode, beginTime, endTime);
  //关闭
  layer.close(index);
});
$(".searchs").click();
}


function getLocalTime(param) {
    var date = new Date(param);
    var content = $.fn.dlshouwen.grid.tools.dateFormat(date, "yyyy-MM-dd");
    return content;
}